﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Common;

namespace WebApi
{
    public class ContactsController : ApiController
    {
        static List<Contact> _contacts;
        static ContactsController()
        {
            _contacts = new List<Contact>();

            _contacts.Add(new Contact
            {
                Id = "001",
                Name = "张三",
                PhoneNo = "15627900919",
                EmailAddress = "pauldng@qq.com",
                Address = "江苏省苏州市星湖街328号"
            });
            _contacts.Add(new Contact
            {
                Id = "002",
                Name = "李四",
                PhoneNo = "15627900902",
                EmailAddress = "tchelp@qq.com",
                Address = "江苏省苏州市金街大道328号"
            });
        }

        public IEnumerable<Contact> Get(string id = null)
        {
            return from contact in _contacts
                   where contact.Id == id || string.IsNullOrEmpty(id)
                   select contact;
        }

        public void Post(Contact contact)
        {
            contact.Id = (_contacts.Select(m => Convert.ToInt32(m.Id)).Max()+1).ToString("D3");
            _contacts.Add(contact);
        }

        public void Put(Contact contact)
        {
            _contacts.Remove(_contacts.First(c => c.Id == contact.Id));
            _contacts.Add(contact);
        }

        public void Delete(string id)
        {
            _contacts.Remove(_contacts.First(c => c.Id == id));
        }
    }
}
